package com.infore.airsupervise.biz.impl;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.serializer.PropertyFilter;
import com.infore.airsupervise.app.Constants;
import com.infore.airsupervise.app.MsgConst;
import com.infore.airsupervise.app.StatusConst;
import com.infore.airsupervise.bean.LawingSiteE;
import com.infore.airsupervise.bean.UserE;
import com.infore.airsupervise.biz.SiteLawingBiz;
import com.infore.airsupervise.http.BaseResponse;
import com.infore.airsupervise.http.HttpAPI;
import com.infore.airsupervise.http.HttpCallBack;
import com.infore.airsupervise.http.HttpUtil;
import com.infore.airsupervise.http.OnServerResponseListener;
import com.infore.airsupervise.utils.JSONUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MultipartBody;
import retrofit2.Call;



/**
 * @features:
 * @author: WuTao
 * @date: 2017/5/15 11:20
 */
public class SiteLawingBizImpl  implements SiteLawingBiz{
    @Override
    public Call<BaseResponse> commitSiteLawingRequest(LawingSiteE siteE, final OnServerResponseListener<BaseResponse> listener) {
        List<MultipartBody.Part> files = new ArrayList<>();
        Map<String ,Object> params = new HashMap<>();
        params.put(Constants.USER_ID_KEY, UserE.getUserInfo().getUserId());
        if(!TextUtils.isEmpty(siteE.getMn())) {
            params.put("mn", siteE.getMn());
        }
        if(!TextUtils.isEmpty(siteE.getMissionId())){
            params.put(Constants.MISSION_ID_KEY,siteE.getMissionId());
        }
        //执法取证
        LawingSiteE.Evidence evidence = siteE.getEvidence();
        PropertyFilter propertyFilter = new PropertyFilter() {
            @Override
            public boolean apply(Object object, String name, Object value) {
                boolean flag = true;
                if(name.equalsIgnoreCase("files")){
                    flag = false;
                }
                return flag;
            }
        };
        String str_evidence = JSON.toJSONString(evidence,propertyFilter);
        params.put("obtainEvidence",str_evidence);
        //监察记录
        List<LawingSiteE.RecorderJC> recorderJCList  = siteE.getRecorderJCList();
        String str_notesJC =JSONArray.toJSONString(recorderJCList,true);
        params.put("RecorderJC",str_notesJC);
        //询问笔录
        List<LawingSiteE.NotesAsk> askList = siteE.getNotesAskList();
        String str_ask = JSONArray.toJSONString(askList,true);
        params.put("NotesAsk",str_ask);
        //检查（勘察）笔录
        List<LawingSiteE.NotesJC> checkList = siteE.getNotesJCList();
        String str_check = JSONArray.toJSONString(checkList,true);
        params.put("NotesJC",str_check);
        //企业人员签名
        List<MultipartBody.Part> qySignature_part = HttpUtil.getInstance().filesToBodyParts("qySignature",siteE.getSignatureCom());
        //执法人员
        List<MultipartBody.Part> zfSignature_part = HttpUtil.getInstance().filesToBodyParts("zfSignature",siteE.getSignaturePson());
        //文件
        List<MultipartBody.Part> file_part = HttpUtil.getInstance().filesToBodyParts("files",evidence.getFiles());
        files.addAll(qySignature_part);
        files.addAll(zfSignature_part);
        files.addAll(file_part);
        List<MultipartBody.Part> param_part = HttpUtil.getInstance().paramsToBodyParts(params);
        Call call = HttpUtil.getHttpService().postFiles(HttpAPI.COMMIT_SITE_LAW, files,param_part);
        call.enqueue( new HttpCallBack(){
            @Override
            public void onSuccess(int operationCode, BaseResponse response) {
                super.onSuccess(operationCode, response);
                listener.onSucceed(response,response.getMessage());
            }

            @Override
            public void onFailed(int operationCode, String desc) {
                super.onFailed(operationCode, desc);
                listener.onFailed(operationCode, MsgConst.OPERATION_FAILED,desc);
            }

            @Override
            public void onException(int errorCode, String title, String desc) {
                super.onException(errorCode, title, desc);
                listener.onFailed(errorCode, title, desc);
            }
        });
        return call;
    }

    @Override
    public Call<BaseResponse> sendSiteLedgerListRequest(
            String keyWord,
            String mn ,
            int pageNum ,
            String userId,
            final OnServerResponseListener<List<LawingSiteE>> listener) {
        Map<String ,Object> params = new HashMap<>();
        params.put(Constants.USER_ID_KEY, UserE.getUserInfo().getUserId());
        params.put(Constants.PAGER_NUM_KEY , pageNum);
        if(!TextUtils.isEmpty(mn)){
            params.put("mn" , mn);
        }
        if(!TextUtils.isEmpty(keyWord)){
            params.put("keyWord" , keyWord);
        }

        Call call = HttpUtil.getHttpService().postParams(HttpAPI.GET_SITELEDGER_LIST,params);
        call.enqueue( new HttpCallBack(){
            @Override
            public void onSuccess(int operationCode, BaseResponse response) {
                super.onSuccess(operationCode, response);
                List<LawingSiteE> list = JSONUtil.parseList(response.getData(),Constants.RESPONSE_BEAN_LIST_KEY,LawingSiteE.class);
                listener.onSucceed(list,MsgConst.GET_DATA_SUCCESS);
            }

            @Override
            public void onFailed(int operationCode, String desc) {
                super.onFailed(operationCode, desc);
                listener.onFailed(operationCode, MsgConst.OPERATION_FAILED,desc);
            }

            @Override
            public void onException(int errorCode, String title, String desc) {
                super.onException(errorCode, title, desc);
                listener.onFailed(errorCode, title, desc);
            }
        });
        return call;
    }

    @Override
    public Call<BaseResponse> sendSiteLedgerDeatilRequest(int id, final int state, final OnServerResponseListener listener) {
        Map<String ,Object> params = new HashMap<>();
        params.put("id",id);
        params.put("state",state);
        Call<BaseResponse> call = HttpUtil.getHttpService().postParams(HttpAPI.GET_SITELEDGER_DETAIL,params);
        call.enqueue(new HttpCallBack(){
            @Override
            public void onSuccess(int operationCode, BaseResponse response) {
                super.onSuccess(operationCode, response);
                Class _cls = getClassType(state);
                if(null != _cls) {
                    Object obj =   JSONUtil.parseBean(response.getData(), Constants.RESPONSE_BEAN_KEY, _cls);
                    listener.onSucceed(obj ,MsgConst.GET_DATA_SUCCESS);
                }
            }

            @Override
            public void onFailed(int operationCode, String desc) {
                super.onFailed(operationCode, desc);

                listener.onFailed(operationCode, MsgConst.OPERATION_FAILED,desc);
            }

            @Override
            public void onException(int errorCode, String title, String desc) {
                super.onException(errorCode, title, desc);
                listener.onFailed(errorCode, title, desc);
            }
        });

        return null;
    }

    private Class getClassType(int state){
        switch (state){
            case StatusConst.SITE_LEDGER_CHECK :
                return LawingSiteE.RecorderJC.class ;
            case StatusConst.SITE_LEDGER_ASK :
                return LawingSiteE.NotesAsk.class;

            case StatusConst.SITE_LEDGER_INSPECT :
                return LawingSiteE.NotesJC.class;
            default:
                return null;
        }
    }
}
